package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.domain.Category;
import com.itheima.reggie.domain.Dish;
import com.itheima.reggie.domain.DishFlavor;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.mapper.DishFlavorMapper;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Set;

//菜品的业务层实现类
@Service
@Transactional
@Slf4j
public class DishServiceImpl implements DishService {

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private DishFlavorMapper dishFlavorMapper;

    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    //分页查询
    @Override
    public Page<Dish> findByPage(Integer pageNum, Integer pageSize, String name) {
        //构建菜品条件
        LambdaQueryWrapper<Dish> qw = new LambdaQueryWrapper<>();
        qw.like(name != null && name.length() > 0, Dish::getName, name);
        //开启分页
        Page<Dish> page = new Page<>(pageNum, pageSize);
        //分页查询
        page = dishMapper.selectPage(page, qw);
        //遍历菜品列表
        List<Dish> dishList = page.getRecords();
        for (Dish dish : dishList) {
            //根据菜品id查询口味列表
            List<DishFlavor> flavorList = dishFlavorMapper.selectList(new LambdaQueryWrapper<DishFlavor>().eq(DishFlavor::getDishId, dish.getId()));
            //将口味列放入菜品对象中
            dish.setFlavors(flavorList);
            //根据分类id查询分类信息
            Category category = categoryMapper.selectById(dish.getCategoryId());
            //将分类放入菜品对象中
            dish.setCategoryName(category.getName());
        }
        //返回分页对象
        return page;
    }

    //新增菜品
    @Override
    public void save(Dish dish) {
        //保存菜品的信息
        dishMapper.insert(dish);
        //获取口味列表
        List<DishFlavor> flavorList = dish.getFlavors();
        //遍历口味列表
        for (DishFlavor dishFlavor : flavorList) {
            //设置菜品的id
            dishFlavor.setDishId(dish.getId());
            //保存口味
            dishFlavorMapper.insert(dishFlavor);
        }
        //数据库发生改变了,删除对应的redis缓存
        redisTemplate.delete("dish:" + dish.getCategoryId());
    }

    //修改菜品之回显菜品
    @Override
    public Dish findById(Long id) {
        //根据传回来的id查询菜品
        Dish dish = dishMapper.selectById(id);
        //根据传回来的id查询口味
        LambdaQueryWrapper<DishFlavor> qw = new LambdaQueryWrapper<>();
        qw.eq(DishFlavor::getDishId, id);
        List<DishFlavor> dishFlavors = dishFlavorMapper.selectList(qw);
        //将查询到的口味信息添加到菜品中
        dish.setFlavors(dishFlavors);
        //返回
        return dish;
    }

    //修改菜品
    @Override
    public void update(Dish dish) {
        //保存修改后的菜品
        dishMapper.updateById(dish);
        //将对应的旧的口味信息删除
        LambdaQueryWrapper<DishFlavor> qw = new LambdaQueryWrapper<>();
        qw.eq(DishFlavor::getDishId, dish.getId());
        dishFlavorMapper.delete(qw);
        //遍历新口味,将新的口味信息添加进菜品中
        for (DishFlavor flavor : dish.getFlavors()) {
            //设置菜品id
            flavor.setDishId(dish.getId());
            dishFlavorMapper.insert(flavor);
        }
        //删除redis中所有菜品的缓存
        Set keys = redisTemplate.keys("dish:*");
        redisTemplate.delete(keys);
    }

    //停售菜品   update dish set status = 0 where id = ids
    @Override
    public void stopBuy(Integer status, List<Long> ids) {
        //根据ids查询菜品
        //select * from dish where id in(id1,id2,di3)
        //LambdaQueryWrapper<Dish> qw = new LambdaQueryWrapper<>();
        //qw.in(Dish::getId, ids);
        //List<Dish> dishList = dishMapper.selectList(qw);
        List<Dish> dishes = dishMapper.selectBatchIds(ids);
        //遍历菜品
        for (Dish dish : dishes) {
            //设置状态
            dish.setStatus(status);
            //修改菜品
            dishMapper.updateById(dish);
        }
        //删除redis中所有菜品的缓存
        Set keys = redisTemplate.keys("dish:*");
        redisTemplate.delete(keys);
    }

    //删除菜品
    @Override
    public String delete(List<Long> ids) {
        //不能删的菜品名字
        String noDeleteName = "";
        //根据ids查询菜品
        List<Dish> dishes = dishMapper.selectBatchIds(ids);
        for (Dish dish : dishes) {
            //判断菜品是否为停售
            if (dish.getStatus() == 0) {
            //删除口味
            LambdaQueryWrapper<DishFlavor> qw01 = new LambdaQueryWrapper<>();
            qw01.eq(DishFlavor::getDishId, dish.getId());
            dishFlavorMapper.delete(qw01);
            //菜品停售中,删除菜品信息
            dishMapper.deleteById(dish.getId());
            //dishFlavorMapper.deleteById(dish.getId());
            } else {
                //在售的不能删打印不能删的菜品名字
                noDeleteName = noDeleteName + dish.getName() + ",";
            }
        }
        //删除redis中所有菜品的缓存
        Set keys = redisTemplate.keys("dish:*");
        redisTemplate.delete(keys);
        //返回
        return noDeleteName;
    }

    //新增套餐之回显菜品
    @Override
    public List<Dish> findList(Long categoryId,String name) {
        //创建条件查询对象
        LambdaQueryWrapper<Dish> qw = new LambdaQueryWrapper<>();
        //创建条件查询语句
        qw.eq(categoryId != null,Dish::getCategoryId, categoryId);
        qw.eq(Dish::getStatus, 1);
        qw.like(StringUtils.isNotEmpty(name), Dish::getName, name);
        //调用mapper
        List<Dish> dishList = dishMapper.selectList(qw);
        //返回
        return dishList;
    }

    //点餐端根据分类id和状态码查询菜品列表
    @Override
    public List<Dish> findByCategoryIdAndStatus(Long categoryId, Integer status) {

        //创建返回List
        List<Dish> dishList = null;

        //定义菜品分类的key
        String dishKey = "dish:" + categoryId;

        //先查询redis中是否有
        if (redisTemplate.hasKey(dishKey)) {
            //有如果直接返回结果
            log.info("redis中有,查询缓存");
            dishList = (List<Dish>) redisTemplate.opsForValue().get(dishKey);
            //返回
            return dishList;
        }
        //如果没有
        log.info("redis中没有,查询mysql");
        //创建条件查询对象
        LambdaQueryWrapper<Dish> qw = new LambdaQueryWrapper<>();
        //创建条件查询语句
        qw.eq(Dish::getCategoryId, categoryId);
        qw.eq(Dish::getStatus, status);
        //调用mapper
        dishList = dishMapper.selectList(qw);
        //遍历菜品集合
        for (Dish dish : dishList) {
            //根据菜品对象的口味id查询口味列表
            //创建条件查询对象
            LambdaQueryWrapper<DishFlavor> lqw = new LambdaQueryWrapper<>();
            //创建条件查询语句
            lqw.eq(DishFlavor::getDishId, dish.getId());
            //调用mapper
            List<DishFlavor> dishFlavors = dishFlavorMapper.selectList(lqw);
            //将口味信息设置进菜品中
            dish.setFlavors(dishFlavors);
        }
        //将查询到的结果存到redis中
        redisTemplate.opsForValue().set(dishKey, dishList);
        //返回
        return dishList;
    }
}
